// Water Quality Monitoring Agent
// 水质监测Agent
const Agent = require('./Agent');

class WaterQualityAgent extends Agent {
  constructor() {
    super('WaterQualityAgent', 'Monitors water quality and generates alerts');
    // 监控水质并生成警报
    this.alertThresholds = {
      turbidity: 3.0, // NTU 浊度阈值
      ph: { min: 6.5, max: 8.5 }, // pH范围
      residualChlorine: { min: 0.2, max: 1.0 }, // mg/L 余氯范围
    };
    this.alerts = [];
  }

  // Process water quality data
  // 处理水质数据
  processData() {
    if (!this.data) return;

    // Check for alerts
    // 检查警报
    this.checkTurbidityAlert();
    this.checkPHAlert();
    this.checkResidualChlorineAlert();

    // Log current status
    // 记录当前状态
    console.log(`[${this.name}] Current water quality:`, {
      turbidity: `${this.data.turbidity.toFixed(2)} NTU`,
      ph: this.data.ph.toFixed(2),
      residualChlorine: `${this.data.residualChlorine.toFixed(2)} mg/L`
    });
    
    console.log(`[${this.name}] 当前水质:`, {
      turbidity: `${this.data.turbidity.toFixed(2)} NTU (浊度)`,
      ph: this.data.ph.toFixed(2),
      residualChlorine: `${this.data.residualChlorine.toFixed(2)} mg/L (余氯)`
    });
  }

  // Check for turbidity alert
  // 检查浊度警报
  checkTurbidityAlert() {
    if (this.data.turbidity > this.alertThresholds.turbidity) {
      const alert = {
        type: 'HIGH_TURBIDITY',
        severity: 'WARNING',
        message: `High turbidity detected: ${this.data.turbidity.toFixed(2)} NTU (threshold: ${this.alertThresholds.turbidity} NTU)`,
        messageCN: `检测到高浊度: ${this.data.turbidity.toFixed(2)} NTU (阈值: ${this.alertThresholds.turbidity} NTU)`,
        timestamp: new Date()
      };
      this.addAlert(alert);
    }
  }

  // Check for pH alert
  // 检查pH警报
  checkPHAlert() {
    const ph = this.data.ph;
    if (ph < this.alertThresholds.ph.min || ph > this.alertThresholds.ph.max) {
      const alert = {
        type: 'PH_OUT_OF_RANGE',
        severity: 'WARNING',
        message: `pH out of range: ${ph.toFixed(2)} (range: ${this.alertThresholds.ph.min}-${this.alertThresholds.ph.max})`,
        messageCN: `pH值超出范围: ${ph.toFixed(2)} (范围: ${this.alertThresholds.ph.min}-${this.alertThresholds.ph.max})`,
        timestamp: new Date()
      };
      this.addAlert(alert);
    }
  }

  // Check for residual chlorine alert
  // 检查余氯警报
  checkResidualChlorineAlert() {
    const residualChlorine = this.data.residualChlorine;
    if (residualChlorine < this.alertThresholds.residualChlorine.min) {
      const alert = {
        type: 'LOW_RESIDUAL_CHLORINE',
        severity: 'WARNING',
        message: `Low residual chlorine: ${residualChlorine.toFixed(2)} mg/L (minimum: ${this.alertThresholds.residualChlorine.min} mg/L)`,
        messageCN: `余氯过低: ${residualChlorine.toFixed(2)} mg/L (最小值: ${this.alertThresholds.residualChlorine.min} mg/L)`,
        timestamp: new Date()
      };
      this.addAlert(alert);
    } else if (residualChlorine > this.alertThresholds.residualChlorine.max) {
      const alert = {
        type: 'HIGH_RESIDUAL_CHLORINE',
        severity: 'WARNING',
        message: `High residual chlorine: ${residualChlorine.toFixed(2)} mg/L (maximum: ${this.alertThresholds.residualChlorine.max} mg/L)`,
        messageCN: `余氯过高: ${residualChlorine.toFixed(2)} mg/L (最大值: ${this.alertThresholds.residualChlorine.max} mg/L)`,
        timestamp: new Date()
      };
      this.addAlert(alert);
    }
  }

  // Add alert to the list
  // 将警报添加到列表
  addAlert(alert) {
    // Check if this is a duplicate alert
    // 检查是否为重复警报
    const isDuplicate = this.alerts.some(existingAlert => 
      existingAlert.type === alert.type && 
      existingAlert.message === alert.message);

    if (!isDuplicate) {
      this.alerts.push(alert);
      console.log(`[${this.name}] ALERT: ${alert.message}`);
      console.log(`[${this.name}] 警报: ${alert.messageCN}`);
      // In a real implementation, this could trigger notifications
      // 在实际实现中，这可以触发通知
    }
  }

  // Clear resolved alerts
  // 清除已解决的警报
  clearResolvedAlerts() {
    // In a real implementation, this would check if alerts are still valid
    // 在实际实现中，这将检查警报是否仍然有效
    // For simulation, we'll occasionally clear some alerts
    // 在模拟中，我们会偶尔清除一些警报
    if (this.alerts.length > 0 && Math.random() > 0.7) {
      console.log(`[${this.name}] Clearing resolved alerts`);
      console.log(`[${this.name}] 清除已解决的警报`);
      this.alerts = [];
    }
  }
}

module.exports = WaterQualityAgent;